<?php
/**
 * 微信应用相关操作
*/
class AppController extends BaseController {
    /**
     * 展示出微信应用
    */
    public function actionList() {
        if (Yii::app()->request->isAjaxRequest) {
            $page = Yii::app()->request->getPost('page', 1);
            $limit = Yii::app()->request->getPost('limit', 10);
            $page_data = App::model()->getPageList($page, $limit);
            $this->jsonResponse($page_data);
        } else {
            $this->render('list');
        }
    }

    /**
     * 获取微信应用
     */
    public function actionGetList() {
        $model = App::model();
        $ret = $model->downloadApp();
        if ($ret === false) {
            $this->error($model->getErrors());
        }
        $this->success('微信应用获取成功');
    }

    /**
     * 修改应用秘钥
    */
    public function actionUpdateSecret() {
        if (!Yii::app()->request->isAjaxRequest) {
            return;
        }
        $agent_id = Yii::app()->request->getParam('agent_id');
        $value = Yii::app()->request->getParam('value');
        if (empty($agent_id) || empty($value)) {
            $this->error('参数非法');
        }
        $app = App::model()->findByPk($agent_id);
        if ($app === null) {
            $this->error('应用不存在');
        }
        $app->secret = $value;
        $app->save();
        $this->success('更新成功');
    }
}